package com.itheima.advice;

import com.itheima.mapper.OperateLogMapper;
import com.itheima.pojo.*;
import com.itheima.utils.BusinessException;
import com.itheima.utils.CurrentHolder;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MemberSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Arrays;


@Aspect
@Component
@Slf4j
public class RecordTimeAdvice {
    @Autowired
    private OperateLogMapper operateLogMapper;

    //    @Around("execution(* com..service.*.*(..))")
    @Around("@annotation(com.itheima.advice.OneAnnotation))")
    public Object aoe(ProceedingJoinPoint pjp) throws Throwable {

        long begin = System.currentTimeMillis();
        Object result = pjp.proceed();

        long end = System.currentTimeMillis();
        long time = end - begin;
        String methodName = pjp.getSignature().getName();
        String className = pjp.getSignature().getDeclaringTypeName();
        String string = Arrays.toString(pjp.getArgs());
        String resultString = result.toString();

        OperateLog operateLog = new OperateLog();
        operateLog.setId(null);
        operateLog.setOperateEmpId(getCurrentId());
        operateLog.setOperateTime(LocalDateTime.now());
        operateLog.setClassName(className);
        operateLog.setMethodName(methodName);
        operateLog.setMethodParams(string);
        operateLog.setReturnValue(resultString);
        operateLog.setCostTime(time);

        operateLogMapper.insert(operateLog);
        return result;
    }

    @Around("execution(* com.itheima.controller.LoginController.*(..))")
    public Object log(ProceedingJoinPoint pjp) throws Throwable {
        long begin = System.currentTimeMillis();
        EmpLoginLog empLoginLog = new EmpLoginLog();
        Object[] pjpArgs = pjp.getArgs();
        EmpDto empDto = (EmpDto) pjpArgs[0];
        String username = empDto.getUsername();
        String password = empDto.getPassword();

        empLoginLog.setUsername(username);
        empLoginLog.setPassword(password);

        Result result = null;
        long end;
        try {
            result = (Result) pjp.proceed();
            end = System.currentTimeMillis();
            long time = end - begin;
            empLoginLog.setCostTime(time);
            empLoginLog.setIsSuccess((short) 1);
            operateLogMapper.insertLog(empLoginLog);
            LoginInfo data = (LoginInfo) result.getData();
            String token = data.getToken();
            empLoginLog.setLoginTime(LocalDateTime.now());
            empLoginLog.setJwt(token);
            operateLogMapper.insertLog(empLoginLog);
            return result;
        } catch (BusinessException e) {
            end = System.currentTimeMillis();
            long time = end - begin;
            empLoginLog.setCostTime(time);
            empLoginLog.setIsSuccess((short) 0);
            empLoginLog.setJwt(null);
            operateLogMapper.insertLog(empLoginLog);
            throw new BusinessException("用户名或密码错误");
        }

    }

    public Integer getCurrentId() {
        return CurrentHolder.getUserId();
    }
}
